package com.aaa.service.impl;

import com.aaa.dao.LoginLogDao;
import com.aaa.dao.MenuDao;
import com.aaa.entity.LoginInfo;
import com.aaa.entity.Menu;
import com.aaa.entity.OperLog;
import com.aaa.service.LoginInfoService;
import com.aaa.vo.LoginInfoVo;
import com.aaa.vo.OperLogVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

@Service
public class LoginInfoServiceImpl extends ServiceImpl<LoginLogDao, LoginInfo> implements LoginInfoService {
    @Autowired
    private LoginLogDao loginLogDao;

    @Override
    public void insert(LoginInfo loginInfo) {
        loginLogDao.insert(loginInfo);
    }

    //分页查询
    @Override
    public Result<IPage<LoginInfo>> findByConditions(Integer currentPage, Integer pageSize, LoginInfoVo loginInfoVo) {
        IPage<LoginInfo> page=new Page<>(currentPage,pageSize);
        QueryWrapper<LoginInfo> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(loginInfoVo.getUserName())){
            wrapper.like("user_name",loginInfoVo.getUserName());
        }
        if(StringUtils.hasText(loginInfoVo.getIpAddr())){
            wrapper.like("ip_addr",loginInfoVo.getIpAddr());
        }
        if(StringUtils.hasText(loginInfoVo.getLoginAccount())){
            wrapper.like("login_account",loginInfoVo.getLoginAccount());
        }
        if(StringUtils.hasText(loginInfoVo.getLoginLocation())){
            wrapper.eq("login_location",loginInfoVo.getLoginLocation());
        }
        if(StringUtils.hasText(loginInfoVo.getBrowser())){
            wrapper.eq("browser",loginInfoVo.getBrowser());
        }
        if(StringUtils.hasText(loginInfoVo.getOs())){
            wrapper.eq("os",loginInfoVo.getOs());
        }
        if(Objects.nonNull(loginInfoVo.getInfoId())){
            wrapper.eq("info_id",loginInfoVo.getInfoId());
        }
        if(Objects.nonNull(loginInfoVo.getLoginStatus())){
            wrapper.eq("status",loginInfoVo.getLoginStatus());
        }
        if(Objects.nonNull(loginInfoVo.getDateRange())&&loginInfoVo.getDateRange().length==2){
            wrapper.between("create_time",loginInfoVo.getDateRange()[0],loginInfoVo.getDateRange()[1]);
        }
        IPage<LoginInfo> page1 = loginLogDao.selectPage(page, wrapper);
        return new Result<IPage<LoginInfo>>(2000,"查询用户成功",page1);
    }

    //删除
    @Override
    public boolean delById(Long id) {
        int i = loginLogDao.deleteById(id);
        if (i > 0) {
            return true;
        }
        return false;
    }
}
